package gases.as3.librarys.components.factory
{
	import gases.as3.librarys.components.containui.FormItem;
	import gases.as3.librarys.components.resource.MacroResourceManagaers;
	import gases.as3.librarys.components.swf.StyleConfig;
	import gases.as3.librarys.components.ui.GSkinBox;
	import gases.as3.librarys.components.ui.GUIComponents;
	import gases.as3.librarys.components.ui.basic.GLabel;
	import gases.as3.librarys.components.ui.container.GAlertPanel;
	import gases.as3.librarys.components.ui.container.GPanel;
	import gases.as3.librarys.components.ui.container.GPanelWithTitle;
	import gases.as3.librarys.components.ui.container.GScreenPanel;
	import gases.as3.librarys.components.ui.container.GTitle;
	import gases.as3.librarys.components.ui.control.GButton;
	import gases.as3.librarys.components.ui.control.GCheckBox;
	import gases.as3.librarys.components.ui.control.GComboBox;
	import gases.as3.librarys.components.ui.control.GLabelButton;
	import gases.as3.librarys.components.ui.control.GList;
	import gases.as3.librarys.components.ui.control.GScrollBar;
	import gases.as3.librarys.components.ui.control.IconCellList;
	import gases.as3.librarys.components.ui.control.Tree;
	import gases.as3.librarys.components.ui.control.group.GButtonGroup;
	import gases.as3.librarys.components.ui.processbar.GProcessBar;
	import gases.as3.librarys.components.ui.text.GTextArea;
	import gases.as3.librarys.components.ui.text.GTextInput;
	import gases.as3.util.ComponentName;

	/**
	 *基础组件的解析 
	 * @author helloworld
	 * 
	 */
	public class BaseComponentsResolve
	{
		public function BaseComponentsResolve()
		{
		}
		
		/**
		 *返回组件 
		 * @param obj
		 * @return 
		 * 
		 */		
		public static function retComponent(obj:Object):GUIComponents
		{
			var gui:GUIComponents;
			switch(obj["localname"].toString())
			{
				case ComponentName.BUTTON:
					gui = addLabelButton(obj);
					break;
				case ComponentName.IMAGEBUTTON:
					gui = addImageButton(obj);
					break;
				case ComponentName.LABEL:
					gui = addLabel(obj);
					break;
				case ComponentName.IMAGE:
					gui = addSkinBox(obj);
					break;
				case ComponentName.INPUT:
					gui = addTextInput(obj);
					break;
				case ComponentName.PANEL:
					gui = addPanel(obj);
					break;
				case ComponentName.PANEL_WITH_TITLE:
					gui = addPanelWithTitle(obj);
					break;
				case ComponentName.TITLE:
					gui = addTitle(obj);
					break;
				case ComponentName.CHECKBOX:
					gui = addCheckBox(obj);
					break;
				case ComponentName.COMBOBOX:
					gui = addComboBox(obj);
					break;
				case ComponentName.FORMITEM:
					gui = addFormItem(obj);
					break;
				case ComponentName.ICONLIST:
					gui = addIconCellList(obj);
					break;
				case ComponentName.LIST:
					gui = addList(obj);
					break;
				case ComponentName.SCROLLBAR:
					gui = addScrollBar(obj);
					break;
				case ComponentName.TREE:
					gui = addTree(obj);
					break;
				case ComponentName.SCREENPANEL:
					gui = addScreenPane(obj);
					break;
				case ComponentName.ALERTPANEL:
					gui = addAlertPane(obj);
					break;
				case ComponentName.COMPONENT:
					gui = addGUIComponents(obj);
					break;
				case ComponentName.BUTTONGROUP:
					gui = addButtonGroup(obj);
					break;
				case ComponentName.TEXTAREA:
					gui = addTextArea(obj);
					break;
				case ComponentName.PROCESSBAR:
					gui = addProcessBar(obj);
					break;
			}
			return gui;
		}
		
		
		public static function addGUIComponents(obj:Object):GUIComponents
		{
			if(!obj)
			{
				return null;
			}
			var ret:GUIComponents = new GUIComponents();
			ret.id = obj.id.toString();
			ret.componentName = obj.name.toString();
			ret.x = parseFloat(obj.x.toString());
			ret.y = parseFloat(obj.y.toString());
			return ret;
		}
		
		public static function addLabelButton(obj:Object):GLabelButton
		{
			var button:GLabelButton = new GLabelButton(obj.text.toString(),obj.textcolor,parseFloat(obj.width.toString()),parseFloat(obj.height.toString()),parseInt(obj.size.toString()));
			button.skin = StyleConfig.instance.retSkin(obj.style.toString());
			button.selectState = retBoolState(obj.selectstate.toString());
			button.isSelect = retBoolState(obj.isselect.toString());
			button.x =  parseFloat(obj.x.toString());
			button.y =  parseFloat(obj.y.toString());
			button.id = obj.id.toString();
			button.componentName = obj.name.toString();
			return button;
		}
		
		public static function addImageButton(obj:Object):GButton
		{
			var button:GButton = new GButton(parseFloat(obj.width.toString()), parseFloat(obj.height.toString()));
			button.skin = StyleConfig.instance.retSkin(obj.style.toString());
			button.selectState = retBoolState(obj.selectstate.toString());
			button.isSelect = retBoolState(obj.isselect.toString());
			button.x =  parseFloat(obj.x.toString());
			button.y =  parseFloat(obj.y.toString());
			button.id = obj.id.toString();
			button.componentName = obj.name.toString();
			return button;
		}
		
		public static function addLabel(obj:Object):GLabel
		{
			var label:GLabel = new GLabel(obj.text.toString(),obj.textcolor,parseInt(obj.size.toString()),parseFloat(obj.width.toString()),parseFloat(obj.height.toString()));
			label.id = obj.id.toString();
			label.x =  parseFloat(obj.x.toString());
			label.y =  parseFloat(obj.y.toString());
			label.componentName = obj.name.toString();
			label.align = obj.align.toString();
			return label;
		}
		
		public static function addCheckBox(obj:Object):GCheckBox
		{
			var checkbox:GCheckBox = new GCheckBox(obj.text.toString(),obj.textcolor);
			checkbox.id = obj.id.toString();
			checkbox.x =  parseFloat(obj.x.toString());
			checkbox.y =  parseFloat(obj.y.toString());
			checkbox.skin = StyleConfig.instance.retSkin(obj.style.toString());
			checkbox.componentName = obj.name.toString();
			return checkbox;
		}
		
		public static function addComboBox(obj:Object):GComboBox
		{
			var comboBox:GComboBox = new GComboBox(obj.text.toString(), parseFloat(obj.width.toString()),null,retBoolState(obj.showscroll.toString()),parseFloat(obj.showheight.toString()));
			comboBox.id = obj.id.toString();
			comboBox.x =  parseFloat(obj.x.toString());
			comboBox.y =  parseFloat(obj.y.toString());
			comboBox.setSkin(StyleConfig.instance.retSkin(obj.btnstyle.toString()),StyleConfig.instance.retSkin(obj.scrollstyle.toString()));
			comboBox.componentName = obj.name.toString();
			return comboBox;
		}
		
		public static function addTextInput(obj:Object):GTextInput
		{
			var input:GTextInput = new GTextInput(obj.text.toString(),parseFloat(obj.width.toString()),parseFloat(obj.height.toString()));
			input.color = obj.textcolor;
			input.skin = StyleConfig.instance.retSkin(obj.style.toString());
			if(obj.restrict.toString())
			{
				input.restrict = obj.restrict.toString();
			}
			input.x =  parseFloat(obj.x.toString());
			input.y =  parseFloat(obj.y.toString());
			input.id = obj.id.toString();
			input.componentName = obj.name.toString();
			input.isPassword = retBoolState(obj.ispassword.toString());
			return input;
		}
		
		public static function addPanel(obj:Object):GPanel
		{
			var panel:GPanel = new GPanel(parseFloat(obj.width.toString()),parseFloat(obj.height.toString()));
			panel.skin = StyleConfig.instance.retSkin(obj.style.toString());
			panel.x =  parseFloat(obj.x.toString());
			panel.y =  parseFloat(obj.y.toString());
			panel.id = obj.id.toString();
			panel.vAlign = obj.valign.toString();
			panel.hAlign = obj.halign.toString();
			panel.componentName = obj.name.toString();
			return panel;
		}
		
		public static function addTitle(obj:Object):GTitle
		{
			var title:GTitle = new GTitle(parseFloat(obj.width.toString()),parseFloat(obj.height.toString()),obj.text.toString(),obj.textcolor);
			title.skin = StyleConfig.instance.retSkin(obj.style.toString());
			title.x =  parseFloat(obj.x.toString());
			title.y =  parseFloat(obj.y.toString());
			title.id = obj.id.toString();
			title.componentName = obj.name.toString();
			return title;
		}
		
		public static function addPanelWithTitle(obj:Object):GPanelWithTitle
		{
			var pt:GPanelWithTitle = new GPanelWithTitle(parseFloat(obj.panelwidth.toString()),parseFloat(obj.panelheight.toString()),
				parseFloat(obj.titlewidth.toString()),parseFloat(obj.titleheight.toString()),
				obj.text.toString(),obj.textcolor);
			pt.setSkin(StyleConfig.instance.retSkin(obj.panelstyle.toString()),StyleConfig.instance.retSkin(obj.titlestyle.toString()));
			pt.x =  parseFloat(obj.x.toString());
			pt.y =  parseFloat(obj.y.toString());
			pt.componentName = obj.name.toString();
			pt.vAlign = obj.valign.toString();
			pt.hAlign = obj.halign.toString();
			pt.id = obj.id.toString();
			return pt;
		}
		
		public static function addSkinBox(obj:Object):GSkinBox
		{
			var image:GSkinBox = new GSkinBox();
			image.x =  parseFloat(obj.x.toString());
			image.y =  parseFloat(obj.y.toString());
			image.id = obj.id.toString();
			if(retBoolState(obj.iscolor.toString()))
			{
				image.drawColorBackground(parseFloat(obj.width.toString()),parseFloat(obj.height.toString()),obj.color,parseFloat(obj.alpha.toString()));
			}
			else
			{
//				var source:Object = MacroResourceManagaers.instance.UIResManager.getUIImage(obj.source.toString());
//				image.source =  source;//通过加载一个地址返回bitmap or bitmapdata
				image.skin = StyleConfig.instance.retSkin(obj.style.toString());
				image.setHeight(parseFloat(obj.height.toString()));
				image.setWidth(parseFloat(obj.width.toString()));
			}
			image.componentName = obj.name.toString();
			return image;
		}
		
		public static function addScreenPane(obj:Object):GScreenPanel
		{
			var window:GScreenPanel = new GScreenPanel();
			window.x =  parseFloat(obj.x.toString());
			window.y =  parseFloat(obj.y.toString());
			window.id = obj.id.toString();
			if(retBoolState(obj.iscolor.toString()))
			{
				window.drawColorBackground(parseFloat(obj.width.toString()),parseFloat(obj.height.toString()),obj.color,parseFloat(obj.alpha.toString()));
			}
			else
			{
				window.setHeight(parseFloat(obj.height.toString()));
				window.setWidth(parseFloat(obj.width.toString()));
				var source:Object = MacroResourceManagaers.instance.UIResManager.getUIImage(obj.source.toString());
				window.setBackgroundSource(source);//通过加载一个地址返回bitmap or bitmapdata
			}
			window.vAlign = obj.valign.toString();
			window.hAlign = obj.halign.toString();
			window.componentName = obj.name.toString();
			return window;
		}
		
		public static function addAlertPane(obj:Object):GAlertPanel
		{
			var window:GAlertPanel = new GAlertPanel();
			window.x =  parseFloat(obj.x.toString());
			window.y =  parseFloat(obj.y.toString());
			window.id = obj.id.toString();
			if(retBoolState(obj.iscolor.toString()))
			{
				window.drawColorBackground(parseFloat(obj.width.toString()),parseFloat(obj.height.toString()),obj.color,parseFloat(obj.alpha.toString()));
			}
			else
			{
				window.setHeight(parseFloat(obj.height.toString()));
				window.setWidth(parseFloat(obj.width.toString()));
				var source:Object = MacroResourceManagaers.instance.UIResManager.getUIImage(obj.source.toString());
				window.setBackgroundSource(source);//通过加载一个地址返回bitmap or bitmapdata
			}
			window.vAlign = obj.valign.toString();
			window.hAlign = obj.halign.toString();
			window.componentName = obj.name.toString();
			return window;
		}
		
		public static function addFormItem(obj:Object):FormItem
		{
			var formitem:FormItem= new FormItem(parseInt(obj.viewstyle.toString()),obj.labeltext.toString(),
				parseFloat(obj.labelwidth.toString()), parseFloat(obj.labelheight.toString()), obj.inputtext.toString(),
				parseFloat(obj.inputwidth.toString()), parseFloat(obj.inputheight.toString()), parseInt(obj.gap.toString()),obj.labelcolor,obj.inputcolor);
			formitem.id = obj.id.toString();
			formitem.x =  parseFloat(obj.x.toString());
			formitem.y =  parseFloat(obj.y.toString());
			formitem.setSkin(StyleConfig.instance.retSkin(obj.style.toString()));
			formitem.setInputIsPassword(retBoolState(obj.ispassword.toString()));
			if(obj.restrict.toString())
			{
				formitem.setInputRestrict(obj.restrict.toString());
			}
			formitem.componentName = obj.name.toString();
			return formitem;
		}
		
		public static function addTree(obj:Object):Tree
		{
			var tree:Tree = new Tree();
			tree.id = obj.id.toString();
			tree.x =  parseFloat(obj.x.toString());
			tree.y =  parseFloat(obj.y.toString());
			tree.skin = StyleConfig.instance.retSkin(obj.style.toString());
			tree.treeWidth =  parseFloat(obj.width.toString());
			tree.componentName = obj.name.toString();
			return tree;
		}
		
		public static function addScrollBar(obj:Object):GScrollBar
		{
			var scrollBar:GScrollBar = new GScrollBar(parseFloat(obj.width.toString()),parseFloat(obj.height.toString()));
			scrollBar.skin = StyleConfig.instance.retSkin(obj.style.toString());
			scrollBar.x =  parseFloat(obj.x.toString());
			scrollBar.y =  parseFloat(obj.y.toString());
			scrollBar.id = obj.id.toString();
			scrollBar.componentName = obj.name.toString();
			return scrollBar;
		}
		
		public static function addList(obj:Object):GList
		{
			var list:GList = new GList();
			list.x =  parseFloat(obj.x.toString());
			list.y =  parseFloat(obj.y.toString());
			list.id = obj.id.toString();
			list.componentName = obj.name.toString();
			return list;
		}
		
		public static function addIconCellList(obj:Object):IconCellList
		{
			var rows:int = parseInt(obj.row.toString());
			var cols:int = parseInt(obj.col.toString());
			var hgap:int = parseFloat(obj.hgap.toString());
			var vgap:int = parseFloat(obj.vgap.toString());
			var cellwidth:int = parseFloat(obj.cellwidth.toString());
			var cellheight:int = parseFloat(obj.cellheight.toString());
			var iconList:IconCellList = new IconCellList(rows,cols,hgap,vgap,cellwidth,cellheight);
			iconList.skin = StyleConfig.instance.retSkin(obj.style.toString());
			iconList.x =  parseFloat(obj.x.toString());
			iconList.y =  parseFloat(obj.y.toString());
			iconList.id = obj.id.toString();
			iconList.componentName = obj.name.toString();
			return iconList;
		}
		
		public static function addButtonGroup(obj:Object):GButtonGroup
		{
			var buttonGroup:GButtonGroup = new GButtonGroup();
			buttonGroup.x =  parseFloat(obj.x.toString());
			buttonGroup.y =  parseFloat(obj.y.toString());
			return buttonGroup;
		}
		
		public static function addTextArea(obj:Object):GTextArea
		{
			var textarea:GTextArea = new GTextArea(parseFloat(obj.width.toString()),parseFloat(obj.height.toString()),parseInt(obj.size.toString()),obj.textcolor);
			textarea.x =  parseFloat(obj.x.toString());
			textarea.y =  parseFloat(obj.y.toString());
			textarea.text = obj.text.toString();
			textarea.align = obj.align.toString();
			return textarea;
		}
		
		public static function addProcessBar(obj:Object):GProcessBar
		{
			var processbar:GProcessBar = new GProcessBar();
			processbar.x =  parseFloat(obj.x.toString());
			processbar.y =  parseFloat(obj.y.toString());
			processbar.setWH(parseFloat(obj.width.toString()),parseFloat(obj.height.toString()));
			processbar.setBackgroundImage(MacroResourceManagaers.instance.UIResManager.getUIImage(obj.backgroundsource.toString()));
			processbar.setProcessBarImage(MacroResourceManagaers.instance.UIResManager.getUIImage(obj.processbarsource.toString()));
			return processbar;
		}
		
		private static function retBoolState(bool:String):Boolean
		{
			var ret:Boolean = false;
			if(bool == "true")
			{
				ret = true;
			}
			return ret;
		}
	}
}